﻿Public Class DialogUser

    Private Sub btnAddRole_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddPetugas.Click
        If btnAddPetugas.Text = "Add" Then
            btnDelPetugas.Enabled = False
            btnEditPetugas.Enabled = False
            DGVPetugas.Enabled = False
            PanelPetugas.Enabled = True
            TextUsernamePetugas.Text = ""
            TextPassPetugas.Text = ""
            TextNamaPetugas.Text = ""
            'ComRolePetugas.Text = ""
            'TextKodeRolePetugas.Text = 0
            TextHPPetugas.Text = ""
            TextAlamatPetugas.Text = ""
            btnAddPetugas.Text = "Save"
            TextUsernamePetugas.Focus()
        Else
            Dim res As String = ValidasiPetugas()
            If res = "Success" Then
                Dim dt As New DataTable
                If Utils.executeSP("proc_petugas", New Object() {"add", TextUsernamePetugas.Text, TextPassPetugas.Text, TextNamaPetugas.Text, ComRolePetugas.SelectedValue, TextAlamatPetugas.Text, TextHPPetugas.Text}, dt) Then
                    If dt IsNot Nothing Then
                        MessageBox.Show(dt.Rows(0)("pesan").ToString)
                        'Utils.exec_SP("proc_zloguser", New Object() {"add", "proc_petugas|add", TextUsernamePetugas.Text, Session.username})
                    End If
                End If
            Else
                MessageBox.Show(res, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
            End If
            RefreshTablePetugas()
            btnAddPetugas.Text = "Add"
            btnDelPetugas.Enabled = True
            btnEditPetugas.Enabled = True
            DGVPetugas.Enabled = True
            PanelPetugas.Enabled = False
        End If
    End Sub
    Private Function ValidasiPetugas() As String
        If TextUsernamePetugas.Text = "" Then
            Return "Username masih kosong."
        End If
        Return "Success"
    End Function
    Private Sub MbGlassButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MbGlassButton3.Click
        Me.Close()
    End Sub

    Private Sub btnEditPetugas_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditPetugas.Click
        If btnEditPetugas.Text = "Edit" Then
            btnDelPetugas.Enabled = False
            btnAddPetugas.Enabled = False
            'TextKode.Enabled = True
            PanelPetugas.Enabled = True
            btnEditPetugas.Text = "Update"
            TextPassPetugas.Focus()
        Else
            btnEditPetugas.Text = "Edit"
            btnDelPetugas.Enabled = True
            btnAddPetugas.Enabled = True
            PanelPetugas.Enabled = False
            Dim dt As New DataTable
            If Utils.executeSP("proc_petugas", New Object() {"edit", TextUsernamePetugas.Text, TextPassPetugas.Text, TextNamaPetugas.Text, ComRolePetugas.SelectedValue, TextAlamatPetugas.Text, TextHPPetugas.Text}, dt) Then
                If dt IsNot Nothing Then
                    MessageBox.Show(dt.Rows(0)("pesan").ToString)
                    'Utils.exec_SP("proc_zloguser", New Object() {"add", "proc_petugas|edit", TextUsernamePetugas.Text, Session.username})
                End If
            End If
            RefreshTablePetugas()
        End If
    End Sub
    Public Sub RefreshTablePetugas()
        Dim dt As New DataTable
        'Dim dsrole As New DataSet
        If Utils.executeSP("proc_petugas", New Object() {"select", TextUsernamePetugas.Text, TextPassPetugas.Text, TextNamaPetugas.Text, TextKodeRolePetugas.Text, TextAlamatPetugas.Text, TextHPPetugas.Text}, dt) Then
            If dt IsNot Nothing Then
                DGVPetugas.DataSource = dt
            End If
        End If
    End Sub
    Private Sub btnDelPetugas_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelPetugas.Click
        If TextUsernamePetugas.Text = "" Then
            MessageBox.Show("Pilih Data Yang ingin dihapus", "Pesan Error", MessageBoxButtons.OK)
        Else
            Dim retval As Integer = MessageBox.Show("Apakah anda yakin akan menghapus " & TextUsernamePetugas.Text & " ?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
            If retval = DialogResult.Yes Then
                Dim dt As New DataTable
                If Utils.executeSP("proc_petugas", New Object() {"delete", TextUsernamePetugas.Text, TextPassPetugas.Text, TextNamaPetugas.Text, TextKodeRolePetugas.Text, TextAlamatPetugas.Text, TextHPPetugas.Text}, dt) Then
                    If dt IsNot Nothing Then
                        MessageBox.Show(dt.Rows(0)("pesan").ToString)
                        'Utils.exec_SP("proc_zloguser", New Object() {"add", "proc_petugas|delete", TextUsernamePetugas.Text, Session.username})
                    End If
                End If

            End If
            RefreshTablePetugas()
        End If
    End Sub

    Private Sub DGVPetugas_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVPetugas.CellClick
        If e.RowIndex = -1 Then Exit Sub

    End Sub

    Private Sub DGVPetugas_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DGVPetugas.SelectionChanged
        With DGVPetugas
            Try
                TextUsernamePetugas.Text = .CurrentRow.Cells(0).Value.ToString
                TextPassPetugas.Text = ""
                TextNamaPetugas.Text = .CurrentRow.Cells(2).Value.ToString
                ComRolePetugas.Text = .CurrentRow.Cells(3).Value.ToString
                TextKodeRolePetugas.Text = .CurrentRow.Cells(4).Value.ToString
                TextAlamatPetugas.Text = .CurrentRow.Cells(5).Value.ToString
                TextHPPetugas.Text = .CurrentRow.Cells(6).Value.ToString
            Catch

            End Try
        End With
    End Sub
    Public Sub RefreshComPetugas()
        Dim dt As New DataTable
        If Utils.executeSP("proc_role", New Object() {"select", Convert.DBNull, Convert.DBNull}, dt) Then
            If dt IsNot Nothing Then
                ComRolePetugas.DataSource = dt
                ComRolePetugas.DisplayMember = "role"
                ComRolePetugas.ValueMember = "koderole"
                'ComRolePetugas.Text = ""
                If ComRolePetugas.SelectedIndex = -1 Then ComRolePetugas.SelectedIndex = 0
            End If
            'TextBiayaPeriksa.Text = ComJenisPenyakit.SelectedValue
        End If
    End Sub
    Private Sub DialogUser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RefreshComPetugas()
        RefreshTablePetugas()
    End Sub

    Private Sub MbGlassButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MbGlassButton1.Click
        RefreshTablePetugas()
        btnEditPetugas.Text = "Edit"
        'btnDelPetugas.Text = ""
        btnAddPetugas.Text = "Add"
        btnEditPetugas.Enabled = True
        btnDelPetugas.Enabled = True
        btnAddPetugas.Enabled = True
        PanelPetugas.Enabled = False

    End Sub
End Class